package com.studentSystem.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.studentSystem.mapper.StudentMapper;
import com.studentSystem.pojo.Student;
import com.studentSystem.service.StudentService;
import com.studentSystem.utils.PageBeanUtil;
import com.studentSystem.utils.SnowflakeIdWorkerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageBeanUtil<Student> list(Integer pageNum, Integer pageSize, Long classId, Long id) {
        int defaultPageNum = 1;
        int defaultPageSize = 5;

        if (pageNum == null || pageNum <= 0) {
            pageNum = defaultPageNum;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = defaultPageSize;
        }
        PageHelper.startPage(pageNum, pageSize);

        List<Student> students;
        students = studentMapper.list(classId, id);

        PageInfo<Student> pageInfo = new PageInfo<>(students);

        PageBeanUtil<Student> pageBeanUtil = new PageBeanUtil<>();
        pageBeanUtil.setTotal(pageInfo.getTotal());
        pageBeanUtil.setItems(pageInfo.getList());

        return pageBeanUtil;
    }

    @Override
    public void deleteById(Long id) {
        studentMapper.deleteById(id);
    }

    @Override
    public void add(Student student) {
        Long id = new SnowflakeIdWorkerUtil(0,0).nextId();
        student.setId(id);
        studentMapper.add(student);
    }

    @Override
    public void update(Student student) {
        studentMapper.update(student);
    }

    @Override
    public Student getById(Long id) {
        return studentMapper.getById(id);
    }

}
